//
//  AddCustomBillView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/6.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol AddCustomBillViewDelegate {
    @objc optional func selectOperate(index:Int) //选择操作
}

class AddCustomBillView: UIView {
    
    weak var delegate : AddCustomBillViewDelegate?
    
    let typeView = AddNewsView() //自定义类型
    let nameView = AddNewsView() //姓名
    let amountView = AddNewsView() //金额
    let repaymentView = SelectNewsView() //还款日
    let cycleView = SelectNewsView() //还款周期
    let saveButton = UIButton() //保存

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.mineColor()
        
        typeView.text = type
        typeView.placeholder = customType
        self.addSubview(typeView)
        
        nameView.text = name
        nameView.placeholder = inputName
        self.addSubview(nameView)
        
        amountView.text = amount
        amountView.placeholder = inputAmount
        amountView.textView.keyboardType = .numberPad
        self.addSubview(amountView)
        
        repaymentView.title = repaymentDate
        repaymentView.text = KMTool.getCurrentTime()
        repaymentView.selectBtn.tag = 1
        repaymentView.selectBtn.addTarget(self, action: #selector(selectOperate(sender:)), for: .touchUpInside)
        self.addSubview(repaymentView)
        
        cycleView.title = repaymentCycle
        cycleView.text = once
        cycleView.selectBtn.tag = 2
        cycleView.selectBtn.addTarget(self, action: #selector(selectOperate(sender:)), for: .touchUpInside)
        self.addSubview(cycleView)
        
        saveButton.setTitle(save, for: .normal)
        saveButton.backgroundColor = UIColor.red
        saveButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        saveButton.layer.cornerRadius = RATIO_H(maxNum: 25)
        saveButton.layer.masksToBounds = true
        saveButton.tag =  3
        saveButton.addTarget(self, action: #selector(selectOperate(sender:)), for: .touchUpInside)
        self.addSubview(saveButton)
        
        typeView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 60))
        }
        
        nameView.snp.makeConstraints { (make) in
            make.top.equalTo(typeView.snp.bottom).offset(RATIO_H(maxNum: 1))
            make.left.right.equalToSuperview()
            make.height.equalTo(typeView)
        }
        
        amountView.snp.makeConstraints { (make) in
            make.top.equalTo(nameView.snp.bottom).offset(RATIO_H(maxNum: 1))
            make.left.right.equalToSuperview()
            make.height.equalTo(nameView)
        }
        
        repaymentView.snp.makeConstraints { (make) in
            make.top.equalTo(amountView.snp.bottom).offset(RATIO_H(maxNum: 1))
            make.left.right.equalToSuperview()
            make.height.equalTo(amountView)
        }
        
        cycleView.snp.makeConstraints { (make) in
            make.top.equalTo(repaymentView.snp.bottom).offset(RATIO_H(maxNum: 1))
            make.left.right.equalToSuperview()
            make.height.equalTo(repaymentView)
        }
        
        saveButton.snp.makeConstraints { (make) in
            make.top.equalTo(cycleView.snp.bottom).offset(RATIO_H(maxNum: 50))
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.height.equalTo(RATIO_H(maxNum: 50))
        }
    }
    
    //MARK: 选择操作
    @objc fileprivate func selectOperate(sender:UIButton) {
        
        self.delegate?.selectOperate!(index: sender.tag)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
